﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using UnityEditor;
    using UnityEngine;

    internal class StateInspector
    {
        [CompilerGenerated]
        private static bool ShowRequiredFieldFootnote__BackingField;
        private Rect actionPanelRect;
        private bool actionsDirty;
        private bool autoScroll;
        private System.Type draggingNewAction;
        private static FsmStateAction editingActionName;
        private bool editingActions;
        private static bool editingStateName;
        private Rect lastActionRect;
        private static FsmStateAction lastSelectedAction;
        private static FsmStateAction mouseOverAction;
        private static bool mouseOverActionPanel;
        private static Rect mouseOverActionRect;
        private static bool mouseOverActionTitlebar;
        private Vector2 mousePos;
        private Vector2 mousePosInScrollView;
        private static string newActionName;
        private string newStateName;
        private Vector2 scrollPosition;
        private float scrollViewHeight;
        private Rect scrollViewRect;
        private static List<FsmStateAction> selectedActions = new List<FsmStateAction>();
        private Rect selectedRect;
        private bool takeActionScreenshots;

        private static bool ActionExistsInSelectedState(FsmStateAction action)
        {
            if (FsmEditor.SelectedState != null)
            {
                foreach (FsmStateAction action2 in FsmEditor.SelectedState.Actions)
                {
                    if (action2 == action)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public void AddAction(System.Type actionType)
        {
            FsmEditor.Inspector.SetMode(HutongGames.PlayMakerEditor.InspectorMode.StateInspector);
            if (this.SelectedAction != null)
            {
                this.AddActionBeforeSelectedAction(actionType);
            }
            else
            {
                FsmEditor.Builder.AddAction(FsmEditor.SelectedState, actionType);
                this.scrollPosition.y = 100000f;
            }
            FsmEditor.RepaintAll();
        }

        public void AddActionAfterSelectedAction(System.Type actionType)
        {
            FsmEditor.Inspector.SetMode(HutongGames.PlayMakerEditor.InspectorMode.StateInspector);
            FsmStateAction action = FsmEditor.Builder.AddAction(FsmEditor.SelectedState, actionType);
            FsmEditor.Builder.MoveActionAfter(FsmEditor.SelectedState, action, lastSelectedAction);
            FsmEditor.RepaintAll();
        }

        public void AddActionBeforeSelectedAction(System.Type actionType)
        {
            FsmEditor.Inspector.SetMode(HutongGames.PlayMakerEditor.InspectorMode.StateInspector);
            FsmStateAction action = FsmEditor.Builder.AddAction(FsmEditor.SelectedState, actionType);
            FsmEditor.Builder.MoveActionBefore(FsmEditor.SelectedState, action, lastSelectedAction);
            FsmEditor.RepaintAll();
        }

        private static void AddActionToSelection(FsmStateAction action)
        {
            SanityCheckActionSelection();
            if (lastSelectedAction == null)
            {
                lastSelectedAction = action;
            }
            if (!selectedActions.Contains(action))
            {
                selectedActions.Add(action);
            }
        }

        public void AddActionToTop(System.Type actionType)
        {
            FsmEditor.Inspector.SetMode(HutongGames.PlayMakerEditor.InspectorMode.StateInspector);
            MoveActionToTop(FsmEditor.Builder.AddAction(FsmEditor.SelectedState, actionType));
            this.scrollPosition.y = 0f;
            FsmEditor.RepaintAll();
        }

        private static void CopyActions(List<FsmStateAction> actions)
        {
            if (FsmEditor.SelectedState != null)
            {
                SanityCheckActionSelection();
                List<FsmState> states = new List<FsmState>();
                states.Add(FsmEditor.SelectedState);
                FsmBuilder.CopyStatesToClipboard(states);
                FsmState item = FsmBuilder.Clipboard.fsm.States[0];
                item.Fsm = FsmEditor.SelectedFsm;
                item.LoadActions();
                List<FsmStateAction> list = new List<FsmStateAction>();
                int index = 0;
                foreach (FsmStateAction action in FsmEditor.SelectedState.Actions)
                {
                    if (!actions.Contains(action))
                    {
                        list.Add(item.Actions[index]);
                    }
                    index++;
                }
                foreach (FsmStateAction action2 in list)
                {
                    item.Actions = HutongGames.PlayMakerEditor.ArrayUtility.Remove<FsmStateAction>(item.Actions, action2);
                }
                item.SaveActions();
                item.Fsm = FsmEditor.SelectedFsm;
                List<FsmState> list3 = new List<FsmState>();
                list3.Add(item);
                FsmBuilder.CopyStatesToClipboard(list3);
            }
        }

        public static void CopySelectedActions()
        {
            CopyActions(SelectedActions);
        }

        public static void DeleteAllActions()
        {
            if ((FsmEditor.SelectedState.Actions.Length != 0) && EditorUtility.DisplayDialog("Delete All Actions", "Are you sure?", "OK", "Cancel"))
            {
                FsmEditor.Builder.DeleteActions(FsmEditor.SelectedState, FsmEditor.SelectedState.Actions, true);
            }
        }

        public void DeleteSelectedActions([Optional, DefaultParameterValue(true)] bool undo)
        {
            FsmEditor.Builder.DeleteActions(FsmEditor.SelectedState, selectedActions, undo);
        }

        public void DeselectActions()
        {
            lastSelectedAction = null;
            SelectedActions.Clear();
            FsmEditor.Repaint(false);
        }

        private void DoActionGUI(FsmStateAction action)
        {
            if (FsmEditor.SelectedState != null)
            {
                action.State = FsmEditor.SelectedState;
                action.Fsm = FsmEditor.SelectedFsm;
                Color color = GUI.color;
                if ((!DebugFlow.ActiveAndScrubbing && Application.isPlaying) && (!action.Active || !FsmEditor.SelectedState.Active))
                {
                    GUI.color = new Color(1f, 1f, 1f, 0.3f);
                }
                else
                {
                    GUI.contentColor = Color.white;
                }
                EditorGUILayout.BeginVertical(new GUILayoutOption[0]);
                this.DoActionTitlebar(action);
                bool enabled = GUI.enabled;
                if (enabled)
                {
                    GUI.enabled = action.Enabled;
                }
                if (FsmEditorSettings.ShowActionParameters && action.IsOpen)
                {
                    if (FsmEditor.ActionEditor.OnGUI(action, FsmEditorSettings.DebugActionParameters, false))
                    {
                        this.actionsDirty = true;
                    }
                    string str = action.ErrorCheck();
                    if (!string.IsNullOrEmpty(str))
                    {
                        GUILayout.Box(str, FsmEditorStyles.ActionErrorBox, new GUILayoutOption[0]);
                    }
                    List<string> runtimeErrors = FsmErrorChecker.GetRuntimeErrors(action);
                    if (runtimeErrors.Count > 0)
                    {
                        foreach (string str2 in runtimeErrors)
                        {
                            GUILayout.Box(str2, FsmEditorStyles.ActionErrorBox, new GUILayoutOption[0]);
                        }
                    }
                }
                GUI.color = color;
                GUI.enabled = enabled;
                if (FsmEditorSettings.ShowActionParameters)
                {
                    FsmEditorGUILayout.ResetGUIColors();
                    FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
                }
                EditorGUILayout.EndVertical();
                this.UpdateMouseOver(action);
                if ((mouseOverAction == action) && mouseOverActionTitlebar)
                {
                    if (Event.current.type == EventType.MouseDown)
                    {
                        this.SelectAction(action, false);
                        if (Event.current.clickCount > 1)
                        {
                            EditActionName(action);
                        }
                        Event.current.Use();
                    }
                    else if ((Event.current.type == EventType.MouseUp) && !Event.current.shift)
                    {
                        selectedActions.Clear();
                        AddActionToSelection(lastSelectedAction);
                    }
                }
            }
        }

        private void DoActionsListGUI(FsmState state)
        {
            if (state.Actions.Length > 0)
            {
                FsmStateAction action = state.Actions[0];
                if ((action != null) && (action.GetType() == typeof(FsmStateAction)))
                {
                    Debug.LogWarning("Reinit FsmStateAction");
                    FsmEditor.SelectedFsm.Reinitialize();
                }
            }
            if (FsmEditorSettings.ShowHints)
            {
                if (state.Actions.Length == 0)
                {
                    GUILayout.Box("Use the Action Browser to Add Actions to this State.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                }
                else
                {
                    GUILayout.Box((Application.platform == RuntimePlatform.OSXEditor) ? "Edit Action Name: \tDouble Click Action Title\nOpen/Close All: \t\tCmd Click Foldout\nEnable/Disable All: Cmd Click Enable Checkbox" : "Edit Action Name: \tDouble Click Action Title\nOpen/Close All: \t\tCtrl Click Foldout\nEnable/Disable All: Ctrl Click Enable Checkbox", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                }
                FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
            }
            this.scrollPosition = GUILayout.BeginScrollView(this.scrollPosition, new GUILayoutOption[0]);
            GUILayout.Space(1f);
            mouseOverAction = null;
            if (Event.current.isMouse || (Event.current.type == EventType.DragUpdated))
            {
                this.mousePosInScrollView = Event.current.mousePosition;
            }
            ActionEditor.FSMEventTargetContextGlobal = null;
            int actionIndex = 0;
            foreach (FsmStateAction action2 in state.Actions)
            {
                if (action2 != null)
                {
                    this.DoActionGUI(action2);
                    if (this.takeActionScreenshots && (Event.current.type == EventType.Repaint))
                    {
                        DocHelpers.CaptureStateInspectorAction(GUILayoutUtility.GetLastRect(), action2.ToString(), actionIndex);
                    }
                    actionIndex++;
                }
            }
            GUILayout.Space(20f);
            this.DrawInsertLine();
            GUILayout.EndScrollView();
            this.DoAutoScroll();
        }

        private void DoActionsPanel()
        {
            if (DebugFlow.Active && DebugFlow.ActiveAndScrubbing)
            {
                GUILayout.Box(FsmEditorSettings.EnableDebugFlow ? "Debug Flow Mode: Action panel shows variable values when state was entered/exited." : "Debug Flow Mode: Recording of variables and other state information is disabled. Enable this setting globally in Preferences or individually on each FSM.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                GUI.enabled = false;
            }
            GUILayout.BeginVertical(new GUILayoutOption[0]);
            this.DoActionsListGUI(FsmEditor.SelectedState);
            GUILayout.FlexibleSpace();
            GUILayout.EndVertical();
            switch (Event.current.type)
            {
                case EventType.Repaint:
                    this.actionPanelRect = GUILayoutUtility.GetLastRect();
                    if (this.takeActionScreenshots)
                    {
                        this.takeActionScreenshots = false;
                        DocHelpers.EndStateActionListCapture();
                    }
                    break;

                case EventType.ContextClick:
                    GenerateStateSettingsMenu().ShowAsContext();
                    break;
            }
            GUI.enabled = true;
        }

        private void DoActionTitlebar(FsmStateAction action)
        {
            FsmEditorGUILayout.ResetGUIColorsKeepAlpha();
            EditorGUILayout.BeginHorizontal((IsActionSelected(action) && (DragAndDrop.visualMode != DragAndDropVisualMode.Copy)) ? FsmEditorStyles.SelectedRow : FsmEditorStyles.ActionTitle, new GUILayoutOption[0]);
            if (FsmEditorSettings.ShowActionParameters)
            {
                bool openState = FsmEditorGUILayout.ActionFoldout(action.IsOpen);
                if (openState != action.IsOpen)
                {
                    if (EditorGUI.actionKey)
                    {
                        FsmEditor.Builder.OpenAllActions(openState);
                    }
                    else
                    {
                        FsmEditor.Builder.OpenAction(action, openState);
                    }
                }
            }
            else
            {
                GUILayout.Space(2f);
            }
            Color color = GUI.color;
            DrawState state = DebugFlow.ActiveAndScrubbing ? DrawState.Normal : FsmDrawState.GetDrawState(FsmEditor.SelectedFsm, FsmEditor.SelectedState, action);
            Color color2 = FsmEditorStyles.ActionColors[(int) state];
            GUI.color = new Color(color2.r, color2.g, color2.b, color.a);
            bool enabledState = GUILayout.Toggle(action.Enabled, "", FsmEditorStyles.ActionToggle, new GUILayoutOption[0]);
            if (enabledState != action.Enabled)
            {
                if (EditorGUI.actionKey)
                {
                    FsmEditor.Builder.EnableAllActions(enabledState);
                }
                else
                {
                    FsmEditor.Builder.EnableAction(action, enabledState);
                }
            }
            GUIContent content = new GUIContent(FsmEditorUtility.GetActionLabel(action));
            if (editingActionName == action)
            {
                DoEditActionName(action);
            }
            else
            {
                bool flag3 = FsmErrorChecker.ActionHasErrors(action);
                if (!EditorApplication.isPlaying && flag3)
                {
                    GUI.contentColor = FsmEditorStyles.GuiContentErrorColor;
                }
                GUIStyle actionTitle = FsmEditorStyles.ActionTitle;
                if (flag3)
                {
                    actionTitle = FsmEditorStyles.ActionTitleError;
                }
                else if (IsActionSelected(action))
                {
                    actionTitle = FsmEditorStyles.ActionTitleSelected;
                }
                GUILayout.Box(content, actionTitle, new GUILayoutOption[0]);
            }
            GUILayout.FlexibleSpace();
            GUI.color = color;
            GUI.contentColor = Color.white;
            if (FsmEditorGUILayout.HelpButtonSmall(FsmEditorUtility.GetActionTooltip(action)))
            {
                EditorCommands.OpenWikiPage(action);
                this.SelectAction(action, false);
            }
            if (FsmEditorGUILayout.SettingsButton())
            {
                this.GenerateActionContextMenu(action).ShowAsContext();
                this.SelectAction(action, false);
            }
            EditorGUILayout.EndHorizontal();
        }

        private void DoAutoScroll()
        {
            if ((Event.current.type == EventType.Repaint) && this.autoScroll)
            {
                this.scrollViewRect = GUILayoutUtility.GetLastRect();
                this.scrollViewHeight = this.scrollViewRect.height;
                if (this.selectedRect.y < 0f)
                {
                    this.scrollPosition.y += this.selectedRect.y;
                    FsmEditor.Repaint(false);
                }
                else if ((this.selectedRect.y + this.selectedRect.height) > this.scrollViewHeight)
                {
                    this.scrollPosition.y += (this.selectedRect.y + this.selectedRect.height) - this.scrollViewHeight;
                    if (this.scrollPosition.y > this.selectedRect.y)
                    {
                        this.scrollPosition.y = this.selectedRect.y;
                    }
                    FsmEditor.Repaint(false);
                }
                this.autoScroll = false;
            }
        }

        private static void DoBottomPanel()
        {
            FsmEditorGUILayout.ResetGUIColors();
            if (ShowRequiredFieldFootnote)
            {
                FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
                GUI.contentColor = FsmEditorStyles.GuiContentErrorColor;
                GUILayout.Label("* Required Fields", EditorStyles.boldLabel, new GUILayoutOption[0]);
                GUI.contentColor = Color.white;
            }
            FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
            EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.Space(5f);
            bool flag = GUILayout.Toggle(FsmEditorSettings.DebugActionParameters, new GUIContent("Debug", "Show the current value of variables used in Action parameters."), new GUILayoutOption[0]);
            if (flag != FsmEditorSettings.DebugActionParameters)
            {
                FsmEditorSettings.DebugActionParameters = flag;
                FsmEditorSettings.SaveSettings();
            }
            GUILayout.Space(5f);
            bool flag2 = GUILayout.Toggle(FsmEditor.SelectedState.HideUnused, new GUIContent("Hide Unused", "Hide unused optional Action parameters (per state)."), new GUILayoutOption[0]);
            if (flag2 != FsmEditor.SelectedState.HideUnused)
            {
                FsmEditor.SelectedState.HideUnused = flag2;
                FsmEditor.SetFsmDirty(false, false);
            }
            GUILayout.FlexibleSpace();
            if (GUILayout.Button(new GUIContent("Action Browser", "Open the Action Browser to add actions to this State."), new GUILayoutOption[0]))
            {
                FsmEditor.OpenActionWindow();
                GUIUtility.ExitGUI();
            }
            EditorGUILayout.EndHorizontal();
            if (FsmEditorSettings.ShowHints)
            {
                GUILayout.Box("Workflow Tip: Hit ~ to open the Action Browser, start typing to filter the Action list, use Up/Down keys to select an Action, and hit Enter to add it to the State. Actions are inserted before any selected Action in the list.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
            }
            EditorGUILayout.Space();
        }

        private static void DoEditActionName(FsmStateAction action)
        {
            newActionName = EditorGUILayout.TextField(newActionName, new GUILayoutOption[] { GUILayout.MinWidth(210f) });
            if (FsmEditorGUILayout.ResetButton())
            {
                FsmEditor.Builder.ResetActionName(action);
                FsmEditorUtility.UpdateActionTooltip(action);
                editingActionName = null;
                FsmEditor.Repaint(false);
            }
            if (Event.current.isKey)
            {
                if (FsmEditorUtility.EnterKeyPressed())
                {
                    FsmEditor.Builder.RenameAction(action, newActionName);
                    FsmEditorUtility.UpdateActionTooltip(action);
                    editingActionName = null;
                    FsmEditor.Repaint(false);
                }
                if (Event.current.keyCode == KeyCode.Escape)
                {
                    editingActionName = null;
                    FsmEditor.Repaint(false);
                }
            }
        }

        private void DoStateNameGUI(FsmState state)
        {
            GUILayout.BeginHorizontal(FsmEditorStyles.ActionTitle, new GUILayoutOption[0]);
            GUI.SetNextControlName("StateNameTextField");
            this.newStateName = EditorGUILayout.TextField(this.newStateName, new GUILayoutOption[0]);
            if (FsmEditorGUILayout.SettingsButtonPadded())
            {
                GenerateStateSettingsMenu().ShowAsContext();
            }
            GUILayout.EndHorizontal();
            string text = FsmEditor.Builder.ValidateNewStateName(state, this.newStateName);
            if (Event.current.type == EventType.KeyUp)
            {
                if (editingStateName)
                {
                    if (((text == "") && FsmEditorUtility.CommitKeyPressed()) && !FsmEditor.Builder.RenameState(state, this.newStateName))
                    {
                        this.newStateName = state.Name;
                        FsmEditor.Repaint(false);
                    }
                    if (Event.current.keyCode == KeyCode.Escape)
                    {
                        this.newStateName = state.Name;
                        FsmEditor.Repaint(false);
                    }
                }
                editingStateName = GUI.GetNameOfFocusedControl() == "StateNameTextField";
            }
            if (text != "")
            {
                GUILayout.Box(text, FsmEditorStyles.ErrorBox, new GUILayoutOption[0]);
            }
            if (FsmEditorSettings.ShowHints)
            {
                GUILayout.Box("Hit Tab after selecting a State to quickly edit the Name.\nRight click in panel for more options.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
            }
            if (FsmEditorSettings.ShowStateDescription)
            {
                string str2 = FsmEditorGUILayout.TextAreaWithHint(state.Description, "Description...", new GUILayoutOption[] { GUILayout.MinHeight(44f) });
                if (str2 != state.Description)
                {
                    state.Description = str2;
                    FsmBuilder.UpdateStateSize(state);
                    FsmEditor.SetFsmDirty(false, false);
                }
            }
            FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
        }

        private void DrawInsertLine()
        {
            if (mouseOverActionPanel && (DragAndDropManager.mode != DragAndDropManager.DragMode.None))
            {
                GUI.Box((mouseOverAction != null) ? new Rect(0f, mouseOverActionRect.y - 2f, 350f, 4f) : new Rect(0f, this.lastActionRect.y + this.lastActionRect.height, 350f, 4f), "", FsmEditorStyles.InsertLine);
            }
        }

        private void DropNewAction(System.Type actionType)
        {
            FsmStateAction action = FsmEditor.Builder.InsertAction(FsmEditor.SelectedState, actionType, mouseOverAction);
            this.SelectAction(action, false);
            this.autoScroll = true;
        }

        private void DropSelectedActions()
        {
            if (IsActionSelected(mouseOverAction))
            {
                DragAndDropManager.Reset();
            }
            else
            {
                CopySelectedActions();
                if (mouseOverAction == null)
                {
                    FsmEditor.RegisterUndo("Move Actions");
                    this.DeleteSelectedActions(false);
                    PasteActions(false);
                }
                else
                {
                    FsmEditor.RegisterUndo("Move Actions");
                    this.DeleteSelectedActions(false);
                    this.PasteActionsBefore(mouseOverAction, false);
                }
                DragAndDropManager.Reset();
            }
        }

        private static void EditActionName(FsmStateAction action)
        {
            editingActionName = action;
            newActionName = FsmEditorUtility.GetActionLabel(action);
        }

        private static void EditActionName(object userdata)
        {
            FsmStateAction action = (FsmStateAction) userdata;
            EditActionName(action);
        }

        public void EditingActions()
        {
            this.editingActions = true;
        }

        private GenericMenu GenerateActionContextMenu(FsmStateAction action)
        {
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("Reset"), false, new GenericMenu.MenuFunction2(StateInspector.ResetAction), action);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Edit Name"), false, new GenericMenu.MenuFunction2(StateInspector.EditActionName), action);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Copy Selected Actions"), false, new GenericMenu.MenuFunction(StateInspector.CopySelectedActions));
            if (FsmBuilder.ClipboardNumStates == 1)
            {
                menu.AddItem(new GUIContent("Paste Actions Before"), false, new GenericMenu.MenuFunction(this.PasteActionsBeforeSelected));
                menu.AddItem(new GUIContent("Paste Actions Replace"), false, new GenericMenu.MenuFunction(this.PasteActionsReplaceSelected));
                menu.AddItem(new GUIContent("Paste Actions After"), false, new GenericMenu.MenuFunction(this.PasteActionsAfterSelected));
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Paste Actions"));
            }
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Move Action To Top"), false, new GenericMenu.MenuFunction2(StateInspector.MoveActionToTop), action);
            menu.AddItem(new GUIContent("Move Action To Bottom"), false, new GenericMenu.MenuFunction2(StateInspector.MoveActionToBottom), action);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Remove Action"), false, new GenericMenu.MenuFunction2(StateInspector.RemoveAction), action);
            return menu;
        }

        private static GenericMenu GenerateStateSettingsMenu()
        {
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("Show State Description"), FsmEditorSettings.ShowStateDescription, new GenericMenu.MenuFunction(StateInspector.ToggleShowStateDescription));
            menu.AddItem(new GUIContent("Show Action Parameters"), FsmEditorSettings.ShowActionParameters, new GenericMenu.MenuFunction(StateInspector.ToggleShowDetails));
            menu.AddItem(new GUIContent("Dim Unused Parameters"), FsmEditorSettings.DimUnusedActionParameters, new GenericMenu.MenuFunction(StateInspector.ToggleDimUnusedParameters));
            int userData = 0;
            foreach (string str in FsmEditorUtility.StateColorNames)
            {
                menu.AddItem(new GUIContent("Set State Color/" + str), FsmEditor.SelectedState.ColorIndex == userData, new GenericMenu.MenuFunction2(EditorCommands.SetStateColorIndex), userData);
                userData++;
            }
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Select All Actions"), false, new GenericMenu.MenuFunction(StateInspector.SelectAllActions));
            if (selectedActions.Count > 0)
            {
                menu.AddItem(new GUIContent("Copy Selected Actions"), false, new GenericMenu.MenuFunction(StateInspector.CopySelectedActions));
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Copy Selected Actions"));
            }
            if (FsmBuilder.ClipboardNumStates == 1)
            {
                menu.AddItem(new GUIContent("Paste Actions"), false, new GenericMenu.MenuFunction(StateInspector.PasteActions));
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Paste Actions"));
            }
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Delete All Actions"), false, new GenericMenu.MenuFunction(StateInspector.DeleteAllActions));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Action Browser"), false, new GenericMenu.MenuFunction(FsmEditor.OpenActionWindow));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Online Help"), false, new GenericMenu.MenuFunction(StateInspector.OpenOnlineHelp));
            return menu;
        }

        private void HandeKeyboardInput()
        {
        }

        private void HandleDragAndDrop()
        {
            mouseOverActionPanel = this.actionPanelRect.Contains(this.mousePos);
            if (mouseOverActionPanel)
            {
                EventType type = Event.current.type;
                if ((type == EventType.MouseDrag) && !FsmEditor.GraphView.IsDragging)
                {
                    if ((mouseOverAction != null) && mouseOverActionTitlebar)
                    {
                        StartDragSelectedActions();
                    }
                }
                else if ((type == EventType.DragUpdated) || (type == EventType.DragPerform))
                {
                    this.UpdateDragAndDrop();
                }
            }
        }

        public static bool IsActionSelected(FsmStateAction action)
        {
            return SelectedActions.Contains(action);
        }

        private static void MoveActionToBottom(object userdata)
        {
            FsmStateAction action = (FsmStateAction) userdata;
            FsmEditor.Builder.MoveActionToBottom(FsmEditor.SelectedState, action);
            FsmEditor.ResetKeyboard();
        }

        private static void MoveActionToTop(object userdata)
        {
            FsmStateAction action = (FsmStateAction) userdata;
            FsmEditor.Builder.MoveActionToTop(FsmEditor.SelectedState, action);
            FsmEditor.ResetKeyboard();
        }

        public void OnGUI()
        {
            if (ValidateSelection())
            {
                if (Event.current.isMouse)
                {
                    this.mousePos = Event.current.mousePosition;
                    if (DragAndDropManager.mode != DragAndDropManager.DragMode.None)
                    {
                        DragAndDropManager.Reset();
                    }
                }
                if (Event.current.type == EventType.DragUpdated)
                {
                    this.mousePos = Event.current.mousePosition;
                }
                this.DoStateNameGUI(FsmEditor.SelectedState);
                this.DoActionsPanel();
                DoBottomPanel();
                this.HandeKeyboardInput();
                this.HandleDragAndDrop();
                if (Event.current.type == EventType.MouseDown)
                {
                    this.DeselectActions();
                    if (Event.current.clickCount > 1)
                    {
                        FsmEditor.OpenActionWindow();
                    }
                }
            }
        }

        private static void OpenOnlineHelp()
        {
            EditorCommands.OpenWikiPage(WikiPages.StateInspector);
        }

        private static void PasteActions()
        {
            PasteActions(true);
        }

        private static void PasteActions(bool undo)
        {
            selectedActions = FsmBuilder.PasteActionsFromTemplate(FsmBuilder.Clipboard, FsmEditor.SelectedState, FsmEditor.SelectedState.Actions.Length, undo);
        }

        public void PasteActionsAfterSelected()
        {
            this.PasteActionsAfterSelected(true);
        }

        public void PasteActionsAfterSelected(bool undo)
        {
            if (this.SelectedAction == null)
            {
                selectedActions = FsmBuilder.PasteActionsFromTemplate(FsmBuilder.Clipboard, FsmEditor.SelectedState, FsmEditor.SelectedState.Actions.Length, undo);
            }
            else
            {
                int actionIndex = FsmEditorUtility.GetActionIndex(FsmEditor.SelectedState, this.SelectedAction);
                selectedActions = FsmBuilder.PasteActionsFromTemplate(FsmBuilder.Clipboard, FsmEditor.SelectedState, actionIndex + 1, undo);
            }
        }

        private void PasteActionsBefore(FsmStateAction action, [Optional, DefaultParameterValue(true)] bool undo)
        {
            int actionIndex = FsmEditorUtility.GetActionIndex(FsmEditor.SelectedState, action);
            selectedActions = FsmBuilder.PasteActionsFromTemplate(FsmBuilder.Clipboard, FsmEditor.SelectedState, actionIndex, undo);
        }

        private void PasteActionsBeforeSelected()
        {
            this.PasteActionsBeforeSelected(true);
        }

        private void PasteActionsBeforeSelected(bool undo)
        {
            this.PasteActionsBefore(this.SelectedAction, undo);
        }

        private void PasteActionsReplaceSelected()
        {
            this.PasteActionsReplaceSelected(true);
        }

        private void PasteActionsReplaceSelected(bool undo)
        {
            int actionIndex = FsmEditorUtility.GetActionIndex(FsmEditor.SelectedState, this.SelectedAction);
            FsmEditor.SelectedState.Actions = HutongGames.PlayMakerEditor.ArrayUtility.Remove<FsmStateAction>(FsmEditor.SelectedState.Actions, this.SelectedAction);
            selectedActions = FsmBuilder.PasteActionsFromTemplate(FsmBuilder.Clipboard, FsmEditor.SelectedState, actionIndex, undo);
        }

        private static void RemoveAction(object userdata)
        {
            FsmStateAction action = (FsmStateAction) userdata;
            FsmEditor.Builder.DeleteAction(FsmEditor.SelectedState, action);
        }

        private static void RemoveActionFromSelection(FsmStateAction action)
        {
            SanityCheckActionSelection();
            if (selectedActions.Contains(action))
            {
                selectedActions.Remove(action);
            }
            if (lastSelectedAction == action)
            {
                lastSelectedAction = null;
            }
        }

        public void Reset()
        {
            this.newStateName = (FsmEditor.SelectedState != null) ? FsmEditor.SelectedState.Name : "";
            SanityCheckActionSelection();
        }

        private static void ResetAction(object userdata)
        {
            FsmStateAction action = (FsmStateAction) userdata;
            FsmEditor.Builder.ResetAction(action);
        }

        public void ResetScrollPosition()
        {
            this.scrollPosition = Vector2.zero;
        }

        public static void SanityCheckActionSelection()
        {
            if (selectedActions == null)
            {
                selectedActions = new List<FsmStateAction>();
            }
            else if ((FsmEditor.SelectedFsm == null) || (FsmEditor.SelectedState == null))
            {
                selectedActions.Clear();
            }
            else
            {
                List<FsmStateAction> list = new List<FsmStateAction>();
                foreach (FsmStateAction action in selectedActions)
                {
                    if (!ActionExistsInSelectedState(action))
                    {
                        list.Add(action);
                    }
                }
                foreach (FsmStateAction action2 in list)
                {
                    selectedActions.Remove(action2);
                }
            }
        }

        public void SelectAction(FsmStateAction action, [Optional, DefaultParameterValue(false)] bool scrollToFrame)
        {
            editingActionName = null;
            SanityCheckActionSelection();
            if ((Event.current.button == 1) || EditorGUI.actionKey)
            {
                if (!selectedActions.Contains(action))
                {
                    lastSelectedAction = action;
                    selectedActions.Clear();
                    selectedActions.Add(action);
                }
                FsmEditor.Window.Repaint();
                this.GenerateActionContextMenu(action).ShowAsContext();
            }
            else if (Event.current.shift)
            {
                if (Event.current.control)
                {
                    RemoveAction(action);
                }
                else
                {
                    AddActionToSelection(action);
                }
            }
            else if (Event.current.alt)
            {
                RemoveActionFromSelection(action);
            }
            else
            {
                lastSelectedAction = action;
                if (!selectedActions.Contains(action))
                {
                    selectedActions.Clear();
                    AddActionToSelection(action);
                }
            }
            if (scrollToFrame)
            {
                this.autoScroll = true;
            }
        }

        public static void SelectAllActions()
        {
            if (FsmEditor.SelectedState != null)
            {
                foreach (FsmStateAction action in FsmEditor.SelectedState.Actions)
                {
                    AddActionToSelection(action);
                }
            }
        }

        private static void StartDragSelectedActions()
        {
            DragAndDropManager.SetMode(DragAndDropManager.DragMode.MoveActions);
            DragAndDrop.PrepareStartDrag();
            DragAndDrop.objectReferences = new UnityEngine.Object[0];
            DragAndDrop.SetGenericData("MoveActions", selectedActions);
            DragAndDrop.StartDrag("MoveActions");
            Event.current.Use();
        }

        private static void ToggleDimUnusedParameters()
        {
            FsmEditorSettings.DimUnusedActionParameters = !FsmEditorSettings.DimUnusedActionParameters;
            FsmEditorSettings.SaveSettings();
        }

        private static void ToggleShowDetails()
        {
            FsmEditorSettings.ShowActionParameters = !FsmEditorSettings.ShowActionParameters;
            FsmEditorSettings.SaveSettings();
        }

        private static void ToggleShowStateDescription()
        {
            FsmEditorSettings.ShowStateDescription = !FsmEditorSettings.ShowStateDescription;
            FsmEditorSettings.SaveSettings();
        }

        public void Update()
        {
            if (this.actionsDirty && !this.editingActions)
            {
                FsmEditor.SelectedState.SaveActions();
                FsmEditor.SetFsmDirty(true, false);
                this.actionsDirty = false;
            }
            this.editingActions = false;
            ShowRequiredFieldFootnote = false;
        }

        private void UpdateDragAndDrop()
        {
            switch (DragAndDropManager.mode)
            {
                case DragAndDropManager.DragMode.None:
                    break;

                case DragAndDropManager.DragMode.AddAction:
                    this.UpdateDragNewAction();
                    return;

                case DragAndDropManager.DragMode.MoveActions:
                    this.UpdateDragSelectedActions();
                    break;

                default:
                    return;
            }
        }

        private void UpdateDragNewAction()
        {
            DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
            this.draggingNewAction = (System.Type) DragAndDrop.GetGenericData("AddAction");
            if (Event.current.type == EventType.DragPerform)
            {
                DragAndDrop.AcceptDrag();
                if (this.draggingNewAction != null)
                {
                    this.DropNewAction(this.draggingNewAction);
                }
            }
            Event.current.Use();
            FsmEditor.Repaint(false);
        }

        private void UpdateDragSelectedActions()
        {
            DragAndDrop.visualMode = DragAndDropVisualMode.Move;
            if (Event.current.type == EventType.DragPerform)
            {
                DragAndDrop.AcceptDrag();
                this.DropSelectedActions();
            }
            Event.current.Use();
            FsmEditor.Repaint(false);
        }

        private void UpdateMouseOver(FsmStateAction action)
        {
            Rect lastRect = GUILayoutUtility.GetLastRect();
            if (lastRect.Contains(this.mousePosInScrollView))
            {
                mouseOverAction = action;
                mouseOverActionTitlebar = (this.mousePosInScrollView.y - lastRect.y) < 20f;
                mouseOverActionRect = lastRect;
            }
            if (lastSelectedAction == action)
            {
                this.selectedRect = lastRect;
                this.selectedRect.y -= this.scrollPosition.y;
            }
            if (action == FsmEditor.SelectedState.Actions[FsmEditor.SelectedState.Actions.Length - 1])
            {
                this.lastActionRect = lastRect;
                if (this.mousePosInScrollView.y > (this.lastActionRect.y + this.lastActionRect.height))
                {
                    mouseOverAction = null;
                }
            }
        }

        private static bool ValidateSelection()
        {
            if (FsmEditor.SelectedFsm == null)
            {
                GUILayout.FlexibleSpace();
                return false;
            }
            if (FsmEditor.SelectedState != null)
            {
                return true;
            }
            if (FsmEditorSettings.ShowHints)
            {
                GUILayout.Box("Select a State in the Graph View to edit...", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
            }
            if (Event.current.type == EventType.ContextClick)
            {
                FsmEditorGUILayout.GenerateStateSelectionMenu(FsmEditor.SelectedFsm).ShowAsContext();
            }
            GUILayout.FlexibleSpace();
            return false;
        }

        public FsmStateAction SelectedAction
        {
            get
            {
                SanityCheckActionSelection();
                if (selectedActions.Count != 1)
                {
                    return null;
                }
                return selectedActions[0];
            }
        }

        public static List<FsmStateAction> SelectedActions
        {
            get
            {
                SanityCheckActionSelection();
                return selectedActions;
            }
        }

        public static bool ShowRequiredFieldFootnote
        {
            [CompilerGenerated]
            get
            {
                return ShowRequiredFieldFootnote__BackingField;
            }
            [CompilerGenerated]
            set
            {
                ShowRequiredFieldFootnote__BackingField = value;
            }
        }
    }
}

